<template>
  <div class="app-container">
    <div class="search">
      <!-- 搜索表单 -->
      <el-form :model="serchForm" ref="formRef">
        <el-row :gutter="20">
          <el-col :span="8"
            ><div class="grid-content ep-bg-purple" />
            <el-form-item label="联系人" prop="account">
              <el-input
                v-model="serchForm.model.tenantContact"
                placeholder="请输入联系人"
                style="width: 200px"
              ></el-input> </el-form-item
          ></el-col>

          <el-col :span="8"
            ><div class="grid-content ep-bg-purple" />
            <el-form-item label="账号" prop="account">
              <el-input
                v-model="serchForm.model.account"
                placeholder="账号"
                style="width: 200px"
              ></el-input> </el-form-item
          ></el-col>

          <!-- <el-col :span="8">
            <el-form-item label="授权状态">
              <el-select
                v-model="serchForm.model.status"
                placeholder="请选择"
                size="mini"
              >
                <el-option label="禁用" :value="0"></el-option>
                <el-option label="正常" :value="1"></el-option>
                <el-option label="审核中" :value="2"></el-option>
                <el-option label="审批通过" :value="3"></el-option>
                <el-option label="审批驳回不通过" :value="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="8"
            ><div class="grid-content ep-bg-purple" />
            <el-form-item label="创建时间" prop="createAt">
              <el-date-picker
                v-model="serchForm.createAt"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间" /></el-form-item
          ></el-col> -->
        </el-row>
      </el-form>
      <!--重置 搜搜按钮 -->
      <div class="btns">
        <el-button icon="Refresh" @click="handleResetClick">重置</el-button>
        <el-button type="primary" icon="Search" @click="handleQueryClick"
          >查询</el-button
        >
        <!-- <el-button type="primary" @click="dialogFormVisible = true"
          ><el-icon><Upload /></el-icon>新增</el-button
        > -->
      </div>
    </div>

    <!-- 表单 -->
    <div class="content">
      <div class="content">
        <div class="table">
          <el-table :data="dataList" border style="width: 100%">
            <el-table-column
              align="center"
              width="180"
              prop="name"
              label="企业LOGO "
            >
              <template #default="scope">
                <el-image
                  style="width: 50px; height: 50px"
                  preview-teleported
                  :src="scope.row.logoUrl"
                  :zoom-rate="1.2"
                  :max-scale="7"
                  :min-scale="0.2"
                  :preview-src-list="[scope.row.logoUrl]"
                  :initial-index="4"
                  fit="cover"
                />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="tenantContact"
              label="联系人"
              width="100"
            />
            <el-table-column
              align="center"
              prop="tenantName"
              label="企业名称"
              width="120"
            />
            <el-table-column
              align="center"
              prop="account"
              label="账号 "
              width="150"
            />

            <el-table-column
              align="center"
              prop="businessType"
              label="企业类型"
              width="120"
            />
            <el-table-column
              align="center"
              prop="duties"
              label="申请人职务"
              width="120"
            />
            <el-table-column
              align="center"
              prop="landline"
              label="座机"
              width="180"
            />

            <el-table-column
              width="180"
              align="center"
              prop="requirement"
              label="状态"
            >
              <template #default="scope">
                <el-tag
                  v-if="scope.row.status == 0"
                  type="success"
                  size="small"
                >
                  禁用
                </el-tag>
                <el-tag v-if="scope.row.status == 1" type="danger" size="small">
                  正常
                </el-tag>
                <el-tag v-if="scope.row.status == 2" type="danger" size="small">
                  待审核
                </el-tag>
              </template>
            </el-table-column>
            <!-- <el-table-column
              align="center"
              prop="createTime"
              label="申请时间"
              width="200"
            >
              <template #default="scope">
                {{ formatDateTime(scope.row.createTime) }}
              </template>
            </el-table-column> -->

            <el-table-column
              align="center"
              prop="tenantEmail"
              label="联系邮箱"
            />

            <el-table-column
              align="center"
              prop="tenantTel"
              label="联系电话"
              width="120"
            />

            <el-table-column align="center" label="操作">
              <template #default="scope">
                <el-row>
                  <el-button
                    size="small"
                    type="primary"
                    text
                    @click="handelOpenDetail(scope.row, 3)"
                    >通过</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    text
                    @click="handelOpenDetail(scope.row, 4)"
                    >拒绝</el-button
                  >
                </el-row>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            v-model:current-page="serchForm.current"
            v-model:page-size="serchForm.size"
            :page-sizes="[10, 20, 30]"
            layout="->,total,sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <!-- 对话框 -->
    <el-dialog v-model="dialogFormVisible" title="新建">
      <el-form :model="form" ref="formEl">
        <el-form-item label="应用名称" label-width="110" prop="appName">
          <el-input
            v-model="form.appName"
            autocomplete="off"
            placeholder="请输入应用名称"
          />
        </el-form-item>
        <el-form-item label="购买账号" label-width="110" prop="purchaseAccount">
          <el-input
            v-model="form.purchaseAccount"
            autocomplete="off"
            placeholder="请输入购买账号"
          />
        </el-form-item>
        <el-form-item label="购买账号名称" label-width="110" prop="accountName">
          <el-input
            v-model="form.accountName"
            autocomplete="off"
            placeholder="请输入购买账号名称"
          />
        </el-form-item>
        <el-form-item label="成交时间" label-width="110" prop="transactionTime">
          <el-date-picker
            v-model="form.transactionTime"
            type="datetime"
            placeholder="成交时间"
            format="YYYY/MM/DD HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="使用期限" label-width="110" prop="usagePeriod">
          <el-input v-model="form.usagePeriod" autocomplete="off" />
        </el-form-item>
        <el-form-item label="到期时间" label-width="110" prop="expirationTime">
          <el-date-picker
            v-model="form.expirationTime"
            type="datetime"
            placeholder="到期时间"
            format="YYYY/MM/DD HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="授权状态" label-width="110" prop="status">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { formatDateTime } from "@/utils/index.js";
import { queryListRequest } from "@/api/frontUser/firmUser.js";
import { approveTenantRequest } from "@/api/frontUser/firmUserAudit.js";

import { ElMessage, ElMessageBox } from "element-plus";

const total = ref(0);

const serchForm = reactive({
  current: 1,
  size: 10,
  model: {
    status: 2,
  },
});

onMounted(() => {
  getList();
});

// 列表数据
const dataList = ref([]);
async function getList() {
  const res = await queryListRequest(serchForm);
  console.log(res);
  dataList.value = res.data.records;
  total.value = res.data.total;
}

//重置
function handleResetClick() {
  console.log("reset");
  serchForm.current = 1;
  serchForm.size = 10;
  serchForm.model = {
    status: 2,
  };
  getList();
}
//查询操作
function handleQueryClick() {
  serchForm.current = 1;
  console.log("handleQueryClick", serchForm);
  getList();
}

//分页器size事件
const handleSizeChange = (size) => {
  serchForm.size = size;
  getList();
};
//分页器current事件
const handleCurrentChange = (current) => {
  serchForm.current = current;
  getList();
};

const handelOpenDetail = (row, status) => {
  if (status === 3) {
    ElMessageBox.confirm("确定要审核通过吗?", "提示", {
      confirmButtonText: "确认通过",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        try {
          const { code } = await approveTenantRequest({
            id: row.id,
            commitFlag: status,
          });
          if (code === 200) {
            ElMessage.success(`操作成功`);
            getList();
          } else {
            ElMessage.error(`服务器故障，稍后再试`);
          }
        } catch (error) {}
      })
      .catch(() => {});
  }

  if (status === 4) {
    ElMessageBox.prompt("拒绝原因", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,

      inputErrorMessage: "拒绝原因不能为空",
    })
      .then(async ({ value }) => {
        //判断value不为空，否则提示

        const { code } = await approveTenantRequest({
          id: row.id,
          commitFlag: status,
          approvalPass: value,
        });
        if (code === 200) {
          ElMessage.success(`操作成功`);
          getList();
        } else {
          ElMessage.error(`服务器故障，稍后再试`);
        }
      })
      .catch(() => {});
  }
};
</script>
<style scoped>
label {
  font-weight: 0 !important;
}
.search {
  background-color: #fff;
  padding: 20px;
}
.search .btns {
  text-align: right;
  padding: 0 50px 10px 0;
}
.pagination {
  margin-top: 14px;
}
</style>
